import configparser
import os
from .path_manager import CONFIG_PATH

def load_db_config():
    """加载数据库配置"""
    config = configparser.ConfigParser()
    try:
        config.read(CONFIG_PATH)
        
        # 检查数据库配置是否存在
        if not config.has_section("database"):
            raise ValueError("配置文件中缺少 [database] 部分")
            
        db_config = {
            "host": config.get("database", "host", fallback="localhost"),
            "user": config.get("database", "user", fallback="root"),
            "password": config.get("database", "password", fallback=""),
            "database": config.get("database", "database", fallback="test_db"),
            "charset": "utf8mb4"
        }
        
        # 获取批量大小
        try:
            batch_size = config.getint("database", "batch_size")
        except (configparser.NoOptionError, ValueError):
            batch_size = 50
            
        return db_config, batch_size
        
    except Exception as e:
        print(f"加载配置错误: {str(e)}")
        print(f"请检查配置文件: {CONFIG_PATH}")
        raise